package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.cart.pojo.*;
import com.dongdongshop.cart.service.OrderService;
import com.dongdongshop.front.mongdb.Article;
import com.dongdongshop.front.mongdb.Comments;
import com.dongdongshop.front.mongdb.TbOrderVo;
import com.dongdongshop.front.pojo.AddressVo;
import com.dongdongshop.mapper.TbOrderItemMapper;
import com.dongdongshop.mapper.TbOrderMapper;
import com.dongdongshop.seller.pojo.TbItem;
import com.dongdongshop.seller.service.GoodsService;
import com.dongdongshop.seller.service.ItemService;
import com.dongdongshop.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Component
@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private TbOrderMapper orderMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private IdWorker idWorker;
    @Autowired
    private TbOrderItemMapper orderItemMapper;
    @Reference
    private ItemService itemService;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void addOrder(String username, AddressVo addressVo, String paymentType) {
        //获取购物车数据
       List<Cart> cartList = (List<Cart>) redisTemplate.boundHashOps("cartList").get(username);
       //获取收货地址
        String adress = addressVo.getProvinceName()+""+addressVo.getCityName()+""+
                addressVo.getAreName()+""+addressVo.getAddress();
        //循环遍历出购物车数据然后生成订单
        for (Cart cart : cartList) {
            long orderId = idWorker.nextId();
            TbOrder order = new TbOrder();
            order.setOrderId(orderId);//订单id
            order.setUserId(username);//用户名
            order.setPaymentType(paymentType);//支付类型
            order.setStatus("1");//状态 未付款
            order.setCloseTime(new Date());//创建时间
            order.setUpdateTime(new Date());// 修改时间
            order.setReceiverAreaName(adress);//收货地址
            order.setReceiverMobile(addressVo.getMobile());//电话
            order.setReceiver(addressVo.getContact());//收货人
            order.setSourceType("2");//订单来源 默认pc端
            order.setSellerId(cart.getSellerId());//商家id
            //循环购物车明细
            double money = 0;
            for (TbOrderItem orderItem : cart.getOrderItemList()) {
                long orderId2 = idWorker.nextId();
                orderItem.setId(orderId2);
                orderItem.setOrderId(orderId);
                orderItem.setSellerId(cart.getSellerId());
                money += orderItem.getTotalFee().doubleValue();//金额累计
                orderItemMapper.insertSelective(orderItem);
            }
            order.setPayment(new BigDecimal(money));
            orderMapper.insertSelective(order);
        }
        //删除redis缓存中的购物车
        redisTemplate.boundHashOps("cartList").delete(username);

    }

    //提交评论
    @Override
    public void insertStatuc(Long orderId, String status, Comments comment) {
        TbOrderExample example = new TbOrderExample();
        TbOrderExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        List<TbOrder> orderList = orderMapper.selectByExample(example);
        TbOrder tbOrder = orderList.get(0);
        tbOrder.setStatus(status);
        orderMapper.updateByPrimaryKeySelective(tbOrder);
        TbOrderItemExample example1 = new TbOrderItemExample();
        TbOrderItemExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andOrderIdEqualTo(orderId);
        List<TbOrderItem> orderItems1 = orderItemMapper.selectByExample(example1);
        TbOrderItem tbOrderItem = orderItems1.get(0);
        TbItem tbItem = itemService.getItemByid(tbOrderItem.getItemId());
        comment.setCname(tbOrder.getBuyerNick());
        comment.set_id(new Random().nextLong());
        comment.setDateTime(new Date());
        comment.setGoodId(tbOrderItem.getGoodsId());
        comment.setOrderItemtitle(tbOrderItem.getTitle());
        comment.setItemId(tbOrderItem.getItemId());
        comment.setLike(1);
        comment.setUserId(tbOrder.getUserId());
        comment.setSpec(tbItem.getSpec());
        mongoTemplate.insert(comment);
    }

    @Override
    public List<TbOrderVo> selectStatuc(String username, String statuc) {
        TbOrderExample example = new TbOrderExample();
        TbOrderExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(statuc);
        criteria.andUserIdEqualTo(username);
        List<TbOrder> orderList = orderMapper.selectByExample(example);
        List<TbOrderVo> orderVos = new ArrayList<>();
        for (TbOrder tbOrder : orderList) {
            TbOrderVo orderVo = new TbOrderVo();
            orderVo.setTbOrder(tbOrder);
            TbOrderItemExample example1 = new TbOrderItemExample();
            TbOrderItemExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andOrderIdEqualTo(tbOrder.getOrderId());
            List<TbOrderItem> orderItems1 = orderItemMapper.selectByExample(example1);
            orderVo.setOrderItems(orderItems1);
            orderVos.add(orderVo);
        }
        return orderVos;
    }
}
